
package jagex;

import java.io.*;
import java.net.URL;

public class DataOperations {

	public static InputStream streamFromPath(String arg0) throws IOException {
		Object obj;
		if (kdb == null) {
			obj = new FileInputStream(arg0);
		} else {
			URL url = new URL(kdb, arg0);
			obj = url.openStream();
		}
		return ((InputStream) (obj));
	}

	public static void readFromPath(String path, byte arg1[], int length) throws IOException {
		InputStream inputstream = streamFromPath(path);
		DataInputStream datainputstream = new DataInputStream(inputstream);
		try {
			datainputstream.readFully(arg1, 0, length);
		} catch (EOFException _ex) {
		}
		datainputstream.close();
	}

	private static char characters[] = { ' ', 'e', 't', 'a', 'o', 'i', 'h',
			'n', 's', 'r', 'd', 'l', 'u', 'm', 'w', 'c', 'y', 'f', 'g', 'p',
			'b', 'v', 'k', 'x', 'j', 'q', 'z', '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', ' ', '!', '?', '.', ',', ':', ';', '(', ')',
			'-', '&', '*', '\\', '\'', '@', '#', '+', '=', '\243', '$', '%',
			'"', '[', ']' };

	public static String byteToString(byte[] data, int offset, int length) {
		char[] buffer = new char[100];
		try {
			int k = 0;
			int l = -1;
			for (int i1 = 0; i1 < length; i1++) {
				int j1 = data[offset++] & 0xff;
				int k1 = j1 >> 4 & 0xf;
				if (l == -1) {
					if (k1 < 13) {
						buffer[k++] = characters[k1];
					} else {
						l = k1;
					}
				} else {
					buffer[k++] = characters[((l << 4) + k1) - 195];
					l = -1;
				}
				k1 = j1 & 0xf;
				if (l == -1) {
					if (k1 < 13) {
						buffer[k++] = characters[k1];
					} else {
						l = k1;
					}
				} else {
					buffer[k++] = characters[((l << 4) + k1) - 195];
					l = -1;
				}
			}
			boolean flag = true;
			for (int l1 = 0; l1 < k; l1++) {
				char c = buffer[l1];
				if (l1 > 4 && c == '@') {
					buffer[l1] = ' ';
				}
				if (c == '%') {
					buffer[l1] = ' ';
				}
				if (flag && c >= 'a' && c <= 'z') {
					buffer[l1] += '\uFFE0';
					flag = false;
				}
				if (c == '.' || c == '!' || c == ':') {
					flag = true;
				}
			}
			return new String(buffer, 0, k);
		} catch (Exception e) {
			e.printStackTrace();
			return ".";
		}
	}

	public static int getUnsignedByte(byte arg0) {
		return arg0 & 0xff;
	}

	public static int readInt(byte abyte0[], int i) {
		return ((abyte0[i] & 0xff) << 24) | ((abyte0[i + 1] & 0xff) << 16)
				| ((abyte0[i + 2] & 0xff) << 8) | (abyte0[i + 3] & 0xff);
	}

	public static int getUnsigned2Bytes(byte abyte0[], int i) {
		return ((abyte0[i] & 0xff) << 8) + (abyte0[i + 1] & 0xff);
	}

	public static int getUnsigned4Bytes(byte arg0[], int arg1)// getUnsigned4Bytes
	{
		return ((arg0[arg1] & 0xff) << 24) + ((arg0[arg1 + 1] & 0xff) << 16)
				+ ((arg0[arg1 + 2] & 0xff) << 8) + (arg0[arg1 + 3] & 0xff);
	}

	public static long getUnsigned8Bytes(byte arg0[], int arg1)// getUnsigned8Bytes
	{
		return (((long) getUnsigned4Bytes(arg0, arg1) & 0xffffffffL) << 32)
				+ ((long) getUnsigned4Bytes(arg0, arg1 + 4) & 0xffffffffL);
	}

	public static int getSigned2Bytes(byte arg0[], int arg1)// getSigned2Bytes
	{
		int i = getUnsignedByte(arg0[arg1]) * 256
				+ getUnsignedByte(arg0[arg1 + 1]);
		if (i > 32767) {
			i -= 0x10000;
		}
		return i;
	}

	public static String addCharacter(String string, int arg1) {
		String s = "";
		for (int i = 0; i < arg1; i++) {
			if (i >= string.length()) {
				s = s + " ";
			} else {
				char c = string.charAt(i);
				if (c >= 'a' && c <= 'z') {
					s = s + c;
				} else if (c >= 'A' && c <= 'Z') {
					s = s + c;
				} else if (c >= '0' && c <= '9') {
					s = s + c;
				} else {
					s = s + '_';
				}
			}
		}

		return s;
	}

	public static long stringLength12ToLong(String arg0) {
		String s = "";
		for (int i = 0; i < arg0.length(); i++) {
			char c = arg0.charAt(i);
			if (c >= 'a' && c <= 'z') {
				s = s + c;
			} else if (c >= 'A' && c <= 'Z') {
				s = s + (char) ((c + 97) - 65);
			} else if (c >= '0' && c <= '9') {
				s = s + c;
			} else {
				s = s + ' ';
			}
		}

		s = s.trim();
		if (s.length() > 12) {
			s = s.substring(0, 12);
		}
		long l = 0L;
		for (int j = 0; j < s.length(); j++) {
			char c1 = s.charAt(j);
			l *= 37L;
			if (c1 >= 'a' && c1 <= 'z') {
				l += (1 + c1) - 97;
			} else if (c1 >= '0' && c1 <= '9') {
				l += (27 + c1) - 48;
			}
		}

		return l;
	}

	public static String longToString(long arg0) {
		String s = "";
		while (arg0 != 0L) {
			int i = (int) (arg0 % 37L);
			arg0 /= 37L;
			if (i == 0) {
				s = " " + s;
			} else if (i < 27) {
				if (arg0 % 37L == 0L) {
					s = (char) ((i + 65) - 1) + s;
				} else {
					s = (char) ((i + 97) - 1) + s;
				}
			} else {
				s = (char) ((i + 48) - 27) + s;
			}
		}
		return s;
	}

	public static byte[] pm(String arg0) throws IOException {
		int i = 0;
		int j = 0;
		int k = 0;
		byte abyte0[] = null;
		while (i < 2) {
			try {
				if (i == 1) {
					arg0 = arg0.toUpperCase();
				}
				InputStream inputstream = streamFromPath(arg0);
				DataInputStream datainputstream = new DataInputStream(
						inputstream);
				byte abyte2[] = new byte[6];
				datainputstream.readFully(abyte2, 0, 6);
				j = ((abyte2[0] & 0xff) << 16) + ((abyte2[1] & 0xff) << 8)
						+ (abyte2[2] & 0xff);
				k = ((abyte2[3] & 0xff) << 16) + ((abyte2[4] & 0xff) << 8)
						+ (abyte2[5] & 0xff);
				int l = 0;
				abyte0 = new byte[k];
				int i1;
				for (; l < k; l += i1) {
					i1 = k - l;
					if (i1 > 1000) {
						i1 = 1000;
					}
					datainputstream.readFully(abyte0, l, i1);
				}

				i = 2;
				datainputstream.close();
			} catch (IOException _ex) {
				i++;
			}
		}
		if (k != j) {
			byte abyte1[] = new byte[j];
			DataFileDecrypter.unpackData(abyte1, j, abyte0, k, 0);
			return abyte1;
		} else {
			return abyte0;
		}
	}

	public static int mm(String s, byte abyte0[]) {
		int i = abyte0[0] * 256 + abyte0[1];
		int j = 0;
		s = s.toUpperCase();
		for (int k = 0; k < s.length(); k++) {
			j = (j * 61 + s.charAt(k)) - 32;
		}

		int l = 2 + i * 10;
		for (int i1 = 0; i1 < i; i1++) {
			int j1 = (abyte0[i1 * 10 + 2] & 0xff) * 0x1000000
					+ (abyte0[i1 * 10 + 3] & 0xff) * 0x10000
					+ (abyte0[i1 * 10 + 4] & 0xff) * 256
					+ (abyte0[i1 * 10 + 5] & 0xff);
			int k1 = (abyte0[i1 * 10 + 9] & 0xff) * 0x10000
					+ (abyte0[i1 * 10 + 10] & 0xff) * 256
					+ (abyte0[i1 * 10 + 11] & 0xff);
			if (j1 == j) {
				return l;
			}
			l += k1;
		}

		return 0;
	}

	public static byte[] ym(String arg0, int arg1, byte arg2[], byte arg3[]) {
		int i = arg2[0] * 256 + arg2[1];
		int j = 0;
		arg0 = arg0.toUpperCase();
		for (int k = 0; k < arg0.length(); k++) {
			j = (j * 61 + arg0.charAt(k)) - 32;
		}

		int l = 2 + i * 10;
		for (int i1 = 0; i1 < i; i1++) {
			int j1 = (arg2[i1 * 10 + 2] & 0xff) * 0x1000000
					+ (arg2[i1 * 10 + 3] & 0xff) * 0x10000
					+ (arg2[i1 * 10 + 4] & 0xff) * 256
					+ (arg2[i1 * 10 + 5] & 0xff);
			int k1 = (arg2[i1 * 10 + 6] & 0xff) * 0x10000
					+ (arg2[i1 * 10 + 7] & 0xff) * 256
					+ (arg2[i1 * 10 + 8] & 0xff);
			int l1 = (arg2[i1 * 10 + 9] & 0xff) * 0x10000
					+ (arg2[i1 * 10 + 10] & 0xff) * 256
					+ (arg2[i1 * 10 + 11] & 0xff);
			if (j1 == j) {
				if (k1 != l1) {
					DataFileDecrypter.unpackData(arg3, k1, arg2, l1, l);
				} else {
					for (int i2 = 0; i2 < k1; i2++) {
						arg3[i2] = arg2[l + i2];
					}

				}
				return arg3;
			}
			l += l1;
		}

		return null;
	}

	public static byte[] lm(String arg0, int arg1, byte arg2[]) {
		int i = arg2[0] * 256 + arg2[1];

		int j = 0;
		arg0 = arg0.toUpperCase();
		for (int k = 0; k < arg0.length(); k++) {
			j = (j * 61 + arg0.charAt(k)) - 32;
		}

		int l = 2 + i * 10;
		for (int i1 = 0; i1 < i; i1++) {
			int j1 = (arg2[i1 * 10 + 2] & 0xff) * 0x1000000
					+ (arg2[i1 * 10 + 3] & 0xff) * 0x10000
					+ (arg2[i1 * 10 + 4] & 0xff) * 256
					+ (arg2[i1 * 10 + 5] & 0xff);
			int k1 = (arg2[i1 * 10 + 6] & 0xff) * 0x10000
					+ (arg2[i1 * 10 + 7] & 0xff) * 256
					+ (arg2[i1 * 10 + 8] & 0xff);
			int l1 = (arg2[i1 * 10 + 9] & 0xff) * 0x10000
					+ (arg2[i1 * 10 + 10] & 0xff) * 256
					+ (arg2[i1 * 10 + 11] & 0xff);
			if (j1 == j) {
				byte abyte0[] = new byte[k1 + arg1];
				if (k1 != l1) {
					DataFileDecrypter.unpackData(abyte0, k1, arg2, l1, l);
				} else {
					for (int i2 = 0; i2 < k1; i2++) {
						abyte0[i2] = arg2[l + i2];
					}

				}
				return abyte0;
			}
			l += l1;
		}

		return null;
	}

	public static String censor(String string, boolean arg1) {
		for (int i = 0; i < 2; i++) {
			String s = string;
			odb = 0;
			int j = 0;
			for (int k = 0; k < string.length(); k++) {
				char c = string.charAt(k);
				if (c >= 'A' && c <= 'Z') {
					c = (char) ((c + 97) - 65);
				}
				if (arg1 && c == '@' && k + 4 < string.length()
						&& string.charAt(k + 4) == '@') {
					k += 4;
				} else {
					byte byte0;
					if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9') {
						byte0 = 0;
					} else if (c == '\'') {
						byte0 = 1;
					} else if (c == '\r' || c == ' ' || c == '.' || c == ','
							|| c == '-' || c == '(' || c == ')' || c == '?'
							|| c == '!') {
						byte0 = 2;
					} else {
						byte0 = 3;
					}
					int l = odb;
					for (int i1 = 0; i1 < l; i1++) {
						if (byte0 == 3) {
							if (tdb[i1] > 0
									&& tdb[i1] < pdb[i1] + rdb[i1].length() / 2) {
								tdb[odb] = tdb[i1] + 1;
								sdb[odb] = sdb[i1];
								qdb[odb] = qdb[i1] + 1;
								pdb[odb] = pdb[i1];
								rdb[odb++] = rdb[i1];
								tdb[i1] = -tdb[i1];
							}
						} else {
							char c1 = rdb[i1].charAt(qdb[i1]);
							if (om(c, c1)) {
								qdb[i1]++;
								if (tdb[i1] < 0) {
									tdb[i1] = -tdb[i1];
								}
							} else if ((c == ' ' || c == '\r') && pdb[i1] == 0) {
								qdb[i1] = 0x1869f;
							} else {
								char c2 = rdb[i1].charAt(qdb[i1] - 1);
								if (byte0 == 0 && !om(c, c2)) {
									qdb[i1] = 0x1869f;
								}
							}
						}
					}

					if (byte0 >= 2) {
						j = 1;
					}
					if (byte0 <= 2) {
						for (int j1 = 0; j1 < ldb.length; j1++) {
							if (om(c, ldb[j1].charAt(0))) {
								tdb[odb] = 1;
								sdb[odb] = k;
								qdb[odb] = 1;
								pdb[odb] = 1;
								rdb[odb++] = ldb[j1];
							}
						}

						for (int l1 = 0; l1 < mdb.length; l1++) {
							if (om(c, mdb[l1].charAt(0))) {
								tdb[odb] = 1;
								sdb[odb] = k;
								qdb[odb] = 1;
								pdb[odb] = j;
								rdb[odb++] = mdb[l1];
							}
						}

						if (j == 1) {
							for (int j2 = 0; j2 < ndb.length; j2++) {
								if (om(c, ndb[j2].charAt(0))) {
									tdb[odb] = 1;
									sdb[odb] = k;
									qdb[odb] = 1;
									pdb[odb] = 1;
									rdb[odb++] = ndb[j2];
								}
							}

						}
						if (byte0 == 0) {
							j = 0;
						}
					}
					for (int k1 = 0; k1 < odb; k1++) {
						if (qdb[k1] >= rdb[k1].length()) {
							if (qdb[k1] < 0x1869f) {
								String s1 = "";
								for (int k2 = 0; k2 < string.length(); k2++) {
									if (k2 < sdb[k1] || k2 > k) {
										s1 = s1 + string.charAt(k2);
									} else {
										s1 = s1 + "*";
									}
								}

								string = s1;
							}
							odb--;
							for (int i2 = k1; i2 < odb; i2++) {
								pdb[i2] = pdb[i2 + 1];
								qdb[i2] = qdb[i2 + 1];
								rdb[i2] = rdb[i2 + 1];
								sdb[i2] = sdb[i2 + 1];
								tdb[i2] = tdb[i2 + 1];
							}

							k1--;
						}
					}

				}
			}

			if (string.equalsIgnoreCase(s)) {
				break;
			}
		}

		return string;
	}

	static boolean om(char arg0, char arg1) {
		if (arg0 == arg1) {
			return true;
		}
		if (arg1 == 'i'
				&& (arg0 == 'l' || arg0 == '1' || arg0 == '!' || arg0 == '|'
						|| arg0 == ':' || arg0 == '\246' || arg0 == ';')) {
			return true;
		}
		if (arg1 == 's' && (arg0 == '5' || arg0 == '$')) {
			return true;
		}
		if (arg1 == 'a' && (arg0 == '4' || arg0 == '@')) {
			return true;
		}
		if (arg1 == 'c' && (arg0 == '(' || arg0 == '<' || arg0 == '[')) {
			return true;
		}
		if (arg1 == 'o' && arg0 == '0') {
			return true;
		}
		return arg1 == 'u' && arg0 == 'v';
	}

	public DataOperations() {
	}

	public static URL kdb = null;
	static String ldb[] = { "fuck", "bastard", "lesbian", "prostitut",
			"spastic", "vagina", "retard", "arsehole", "asshole", "tosser",
			"homosex", "hetrosex", "hitler", "urinate" };
	static String mdb[] = { "shit", "lesbo", "phuck", "bitch", "penis",
			"whore", "bisex", "sperm", "rapist", "shag", "slag", "slut",
			"clit", "cunt", "piss", "nazi", "urine" };
	static String ndb[] = { "wank", "naked", "fag", "niga", "nige", "gay",
			"rape", "cock", "homo", "twat", "arse", "crap", "poo" };
	static int odb;
	static int pdb[] = new int[1000];
	static int qdb[] = new int[1000];
	static String rdb[] = new String[1000];
	static int sdb[] = new int[1000];
	static int tdb[] = new int[1000];

	public static int getIntFromByteArray(byte byteArray[], int offset,
			int length) {
		int bitOffset = offset >> 3;
		int bitMod = 8 - (offset & 7);
		int i1 = 0;
		for (; length > bitMod; bitMod = 8) {
			i1 += (byteArray[bitOffset++] & baseLengthArray[bitMod]) << length
					- bitMod;
			length -= bitMod;
		}

		if (length == bitMod) {
			i1 += byteArray[bitOffset] & baseLengthArray[bitMod];
		} else {
			i1 += byteArray[bitOffset] >> bitMod - length
					& baseLengthArray[length];
		}
		return i1;
	}

	private static int baseLengthArray[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255,
			511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 0x1ffff, 0x3ffff,
			0x7ffff, 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff,
			0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff,
			0x7fffffff, -1 };

}
